package com.thyrsus.project.controller.event;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.thyrsus.project.form.event.EventForm;
import com.thyrsus.project.form.user.UserForm;
import com.thyrsus.project.helper.URLHelper;
import com.thyrsus.project.helper.UserHelper;
import com.thyrsus.project.object.EventObject;
import com.thyrsus.project.object.UserContext;
import com.thyrsus.project.service.event.EventService;

@Controller
@SessionAttributes
public class NewEventController {
	
	static Logger LOGGER = Logger.getLogger(DeleteEventController.class);
	
	@Autowired
	private EventService eventService;
	@RequestMapping(value = "/event/new")
	public ModelAndView doNew(HttpServletRequest request,HttpServletResponse response) {
    	if (!UserHelper.isConnected(request)) {
    		return new ModelAndView(URLHelper.getURL(UserHelper.getLoginURL(request, response,"/event/new"),request), "userForm", new UserForm());
    	} else {
    		ModelAndView mav = new ModelAndView (URLHelper.getURL("/event/new",request));
    		mav.addObject("eventForm", new EventForm("/event/new"));
    		return mav;
    	}
    }
	
	@RequestMapping(value = "/event/new", method = RequestMethod.POST)
    public ModelAndView doNew(@ModelAttribute("eventForm")
                            EventForm eventForm, BindingResult result, HttpServletRequest request,HttpServletResponse response) {
		if (!UserHelper.isConnected(request)) {
    		return new ModelAndView(URLHelper.getURL(UserHelper.getLoginURL(request,response, "/event/new"),request), "userForm", new UserForm());
    	} else {
    		if (eventForm.getStep()==1) {
    			LOGGER.debug("Step one of the event creation : "+eventForm.getName());
    			eventForm.increaseStep();
    			eventForm.setAction("/event/create");
    		} else {
    			eventForm.setAction("/event/create");
    		}
    		ModelAndView mav = new ModelAndView (URLHelper.getURL("/event/new",request));
    		mav.addObject("eventForm", eventForm);
    		return mav;
    	}
	}
	
	@RequestMapping(value = "/event/create", method = RequestMethod.POST)
    public String doCreate(@ModelAttribute("eventForm")
                            EventForm eventForm, BindingResult result, HttpServletRequest request,HttpServletResponse response) {
		LOGGER.debug("createEvent : "+eventForm.getName());
		
		UserContext user = UserHelper.getUser(request);
		
		eventService.create(user, new EventObject(eventForm));
		eventService.log();
		LOGGER.debug("Number of guest : "+eventForm.getUserGuests().size());
		
		return "redirect:/home";
	}
}
